package com.itbuka.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itbuka.user.domain.User;
import com.itbuka.user.service.UserService;
import com.itbuka.user.mapper.UserMapper;
import com.itbuka.user.util.JwtUtils;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author apple
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2024-07-29 19:22:32
*/
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper iUserMapper;

@Override
public List<User> selectAll() {
LambdaQueryWrapper <User> lqw = new LambdaQueryWrapper<>();
lqw.eq(User::getIsDelete,0);
return iUserMapper.selectList(lqw);
}


@Override
public List<User> selectList(User iUser) {
LambdaQueryWrapper <User> lqw = this.lqw(iUser);
return iUserMapper.selectList(lqw);
}

@Override
public Integer insert(User iUser) {
    String hashpw = BCrypt.hashpw(iUser.getPassword(), BCrypt.gensalt());
    iUser.setPassword(hashpw);
    return iUserMapper.insert(iUser);
}

@Override
@Transactional
public Integer delete(String ids) {
try {
String[] split = ids.split(",");
for (String id : split) {
User iUser = new User();
iUser.setId(Long.parseLong(id));
iUser.setIsDelete(1);
iUserMapper.updateById(iUser);
}
return 1;
}catch (Exception e){
return -1;
}
}

@Override
public Integer update(User iUser) {
return iUserMapper.updateById(iUser);
}

@Override
public Page<User> pageAll(Integer page, Integer size) {
PageHelper.startPage(page,size);
return (Page<User>)this.selectAll();
}

@Override
public Page<User> pageList(User iUser, Integer page, Integer size) {
PageHelper.startPage(page,size);
return (Page<User>)this.selectList(iUser);
}

    @Override
    public Integer status(Long id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        return iUserMapper.updateById(user);
    }

    @Override
    @Transactional
    public void resetPwd(List<Long> ids) {
        for (Long id : ids) {
            String hashpw = BCrypt.hashpw("123456", BCrypt.gensalt());
            User user = new User();
            user.setId(id);
            user.setPassword(hashpw);
            int i = iUserMapper.updateById(user);
            if (i != 1) {
                throw new RuntimeException("重置密码失败");
            }
        }

    }

    @Override
    public String login(String username, String password) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        lqw.eq(User::getIsDelete, 0);
        User user = iUserMapper.selectOne(lqw);
        if (user == null) {
            return "-2";
        }
        if (user.getStatus() == 1) {
            return "-3";
        }
        if (!BCrypt.checkpw(password, user.getPassword())) {
            return "-1";
        }
        Map map = new HashMap();
        map.put("id",user.getId());
        map.put("username",user.getUsername());
        map.put("role",user.getRole());
        String token = JwtUtils.encrypt(map, new Date(new Date().getTime() + 24 * 3600 * 1000));
        return token;
    }

    public LambdaQueryWrapper lqw(User iUser){
LambdaQueryWrapper <User> lqw = new LambdaQueryWrapper<>();
    if (iUser.getId() != null){
    lqw.eq(User::getId,iUser.getId());
    }
    if (iUser.getUsername() != null){
    lqw.eq(User::getUsername,iUser.getUsername());
    }
    if (iUser.getAvatar() != null){
    lqw.eq(User::getAvatar,iUser.getAvatar());
    }
    if (iUser.getPassword() != null){
    lqw.eq(User::getPassword,iUser.getPassword());
    }
    if (iUser.getPhoneNum() != null){
    lqw.eq(User::getPhoneNum,iUser.getPhoneNum());
    }
    if (iUser.getEmail() != null){
    lqw.eq(User::getEmail,iUser.getEmail());
    }
    if (iUser.getNickname() != null){
    lqw.eq(User::getNickname,iUser.getNickname());
    }
    if (iUser.getDepartment() != null){
    lqw.eq(User::getDepartment,iUser.getDepartment());
    }
    if (iUser.getRole() != null){
    lqw.eq(User::getRole,iUser.getRole());
    }
    if (iUser.getStatus() != null){
    lqw.eq(User::getStatus,iUser.getStatus());
    }
    if (iUser.getIsDelete() != null){
    lqw.eq(User::getIsDelete,iUser.getIsDelete());
    }
    if (iUser.getCreateTime() != null){
    lqw.eq(User::getCreateTime,iUser.getCreateTime());
    }
    if (iUser.getUpdateTime() != null){
    lqw.eq(User::getUpdateTime,iUser.getUpdateTime());
    }
return lqw;
}
}




